package jyj.springmvc.learning.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author 老顽固小可爱
 * @Date 2022/12/12 18:59
 * @Version 1.0
 */
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 目标方法执行前
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        Object loginUser = request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            //未登录，返回登陆页
            request.setAttribute("msg", "您没有权限进行此操作，请先登录！");
            request.getRequestDispatcher("/").forward(request, response);
            return false;
        } else {
            //放行
            return true;
        }
    }
}
